﻿#define GPS_EMULATOR

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Shell;

namespace phoneApp
{
    public partial class RunPage : PhoneApplicationPage
    {
        private Run run;
        private GeoCoordinateWatcher watcher;
        private GeoCoordinate firstCoord;

        public RunPage()
        {
            InitializeComponent();
            Microsoft.Phone.Shell.PhoneApplicationService.Current.ApplicationIdleDetectionMode = Microsoft.Phone.Shell.IdleDetectionMode.Disabled;

            if (watcher == null)
            {
                watcher = new System.Device.Location.GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                //watcher = new GpsEmulatorClient.GeoCoordinateWatcher(GeoPositionAccuracy.Default);
                watcher.MovementThreshold = 1;
                watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
                watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
            }

            startRun();
        }

        private void startRun()
        {
            run = new Run();
            watcher.Start();
            run.addPoint((float)watcher.Position.Location.Longitude, (float)watcher.Position.Location.Latitude);
        }

        void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case GeoPositionStatus.Disabled:
                    //MessageBox.Show("Location Service is not enabled on the device");
                    break;

                case GeoPositionStatus.NoData:
                    //MessageBox.Show(" The Location Service is working, but it cannot get location data.");
                    break;
            }
        }

        void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (e.Position.Location.IsUnknown)
            {
                //this.notification.Text = "Please wait while your prosition is determined....";
                return;
            }

            if (run.track.points.Count == 0)
            {
                firstCoord = e.Position.Location;
            }

            run.addPoint((float)e.Position.Location.Longitude, (float)e.Position.Location.Latitude);
            run.track.length += (float)e.Position.Location.GetDistanceTo(firstCoord);
            distanceText.Text = run.track.length.ToString() + " m";

            TimeSpan span = DateTime.Now.Subtract(run.startDate);
            timeText.Text = span.Minutes.ToString() + " minutes";

            this.runMap.Center = e.Position.Location;

            
            Pushpin locationPushpin = new Pushpin();
            locationPushpin.Style = this.Resources["PushpinStyle"] as Style;

            locationPushpin.Tag = "locationPushpin";
            locationPushpin.Location = watcher.Position.Location;
            this.runMap.Children.Add(locationPushpin);
            this.runMap.SetView(watcher.Position.Location, 18.0);

            CycleTileData cycleTile = new CycleTileData()
            {
                Title = "tytul",
                Count = 5,
                SmallBackgroundImage = new Uri("/Background.png", UriKind.Relative),
                CycleImages = new Uri[]
   {
      new Uri("/NewFolder1/led_square_green.png", UriKind.Relative), 
      new Uri("/NewFolder1/led_square_grey.png", UriKind.Relative), 
      new Uri("/NewFolder1/led_square_orange.png", UriKind.Relative), 
      new Uri("/NewFolder1/led_square_purple.png", UriKind.Relative), 
      new Uri("/NewFolder1/led_square_red.png", UriKind.Relative), 
   }
            };
            
        }
    }
}